package com.tank.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;

public class Pager {

	int pageNo;
	int pageSize;
	int pageCount;
	String pageKey;
	
	public List data;
	
	public List getData() {
		return data;
	}

	public void setData(List data) {
		this.data = data;
	}

	int rowCount;
	String queryString;
	String requestUri;
	int startId;
	
	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public String getPageKey() {
		return pageKey;
	}

	public void setPageKey(String pageKey) {
		this.pageKey = pageKey;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public String getRequestUri() {
		return requestUri;
	}

	public void setRequestUri(String requestUri) {
		this.requestUri = requestUri;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setStartId(int startId) {
		this.startId = startId;
	}

	public Pager(){
		this(10);		
	}
	public Pager(int pageSize) {
		this("pageNo",pageSize);
	}
	public Pager(String pageKey,int pageSize) {
		
		try{
//			this.requestUri="app://";
//			this.queryString="";
			this.requestUri=requestUri=ServletActionContext.getRequest().getRequestURI();
			this.queryString=queryString=ServletActionContext.getRequest().getQueryString();
		}catch(Exception e){
			this.requestUri="";
			this.queryString="";
		}

		this.pageSize=pageSize;
		this.pageKey=pageKey;
		this.queryString=queryString==null?"":queryString;
		this.setQueryString();
		this.setStartId();
	}

	


	private void setStartId() {			
		int startId=(this.pageNo-1)*this.pageSize;
		if(startId<0 ) startId=0;		
		this.startId=startId;
	}
	
	public int getStartId() {			
		return this.startId;
	}
	
	public int getPageSize() {			
		return this.pageSize;
	}
	
	public  List pageQuery(Query query){
		return query.setMaxResults((int) this.pageSize).setFirstResult((int) this.startId).list();
	}
	
	
	private void setQueryString(){
		this.queryString=this.queryString.replaceAll("\\s", "");
		
		if(queryString.length() == 0){
			this.queryString=this.pageKey+"=";
			this.pageNo=1;
			System.out.println("pageNo:"+this.pageNo);
			 return;
		}
		
		
		Pattern pat=Pattern.compile(this.pageKey+"\\=(\\d*)");
		Matcher mat=pat.matcher(this.queryString);
//		System.out.println(pat.pattern());
		if(mat.find()){	
//			System.out.println(mat.group());
//			System.out.println(mat.group(1));
			
			this.queryString=this.queryString.replaceAll("\\&?"+this.pageKey+"\\=(\\d*)(\\&?)", "");
			this.pageNo=Integer.parseInt(mat.group(1));
			System.out.println("pageNo:"+this.pageNo);
		}else{
			this.pageNo=1;
			System.out.println("pageNo:"+this.pageNo);
		}
		this.queryString+="&"+this.pageKey+"=";
	}
	
	
	// <a href="#" title="First Page">&laquo; First</a><a href="#" title="Previous Page">&laquo; Previous</a> <a href="#" class="number" title="1">1</a> <a href="#" class="number" title="2">2</a> <a href="#" class="number current" title="3">3</a> <a href="#" class="number" title="4">4</a> <a href="#" title="Next Page">Next &raquo;</a><a href="#" title="Last Page">Last &raquo;</a> </div>
	public String getPagerHtml(){
		StringBuilder sb=new StringBuilder();
		
		for(int i=1;i<=this.pageCount;i++){
			
			if(i==this.pageNo) {
				sb.append("<a class='number current'  href='"+this.requestUri+"?"+this.queryString+i+"'>"+i+"</a>");	
				continue;
			}
			sb.append("<a class='number'  href='"+this.requestUri+"?"+this.queryString+i+"'>"+i+"</a>");			
		}		
		return sb.toString();
	}
	
	public String getPagerSimpleHtml(){
		StringBuilder sb=new StringBuilder();
		
		for(int i=1;i<=this.pageCount;i++){
			//sb.append("<a href='"+this.requestUri+"?"+this.queryString+i+"'>"+i+"</a>");
			
			if(i==this.pageNo){
				sb.append("<span class=\"current\">"+i+"</span>");
				continue;
			}
			sb.append("<a  href='"+this.requestUri+"?"+this.queryString+i+"'>"+i+"</a>");			

		}		
		return sb.toString();
	}
	
	
	
	
	public static void main(String[] args) {
//		Pager page=new Pager("pageno",10);
//		page.setRowCount(21);
//		System.out.println(page.getPagerHtml());
		List params=new ArrayList();
		params.add(1);
		params.add("hello");
		
		query("select * from Item where id=?",params);
		
	}

	private static void query(String hql, List params) {
		
		int i=hql.indexOf("from");
		String hql2=hql.substring(i);
//		System.out.println(hql2);
		hql2="select count(id) "+hql2;
		Query query;
		
		
	}

	public void setRowCount(Object obj) {
			this.rowCount=Integer.parseInt(obj+"");
			double doubleCount=this.rowCount%this.pageSize;
			//System.out.println(doubleCount);
			int intCount =this.rowCount/this.pageSize;
			this.pageCount=doubleCount>0?intCount+1:intCount;
	}
	
	
}
